<?php
/**
 * Description: 门店管理
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/7/20
 * Time: 18:36
 */

namespace Biz\model\store;

use Biz\service\Store\StoreBasicService;
use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\ImgUrl;

class StoreOtherStoresModel extends BaseDbModel
{
    public const _CONFIG_FILE_ = 'db_store';

    public const _CONFIG_SELECT_ = 'db_store';

    public const _DATABASE_ = 'store';

    public const _TABLE_NAME = 'store_other_stores';

    //状态 ：删除:9 正常:1
    public const DELETED = 9;
    public const NORMAL = 1;

    public const STATUS_ARR = [
        self::DELETED => '删除',
        self::NORMAL => '正常',
    ];
    public const MAX_NUM = 5;

    /**
     * 获取一行
     * @param $where
     * @param string $field
     * @return array|mixed|null
     * @throws \JsonException
     */
    public static function first($where, $field = '*')
    {
        $rs = self::get($field, $where);
        if (!$rs) {
            return null;
        }
        return self::_format($rs);
    }

    /**
     * 批量查询
     * @param $where
     * @param string $field
     * @return array|bool
     * @throws \JsonException
     */
    public static function all($where, $field = '*')
    {
        $rs = self::select($field, $where);
        if ($rs) {
            foreach ($rs as &$r) {
                $r = self::_format($r);
            }
        }
        return $rs;
    }

    /**
     * 获取一列的值
     * @param $where
     * @param array $field
     * @return array|bool
     */
    public static function pluck($where, array $field = ['id'])
    {
        return self::select($field, $where);
    }

    /**
     * 从记录中获取单个值
     * @param $where
     * @param string $columns
     * @return array|bool
     */
    public static function value($where, string $columns = 'id')
    {
        return self::get($columns, $where);
    }

    /**
     * 连表查询
     * @param $table
     * @param $main_col
     * @param $attach_col
     * @param $field
     * @param $where
     * @return array|bool
     */
    public static function left_join($table, $main_col, $attach_col, $field, $where)
    {
        $join = ['[>]' . $table => [$main_col => $attach_col]];
        return self::select($join, $field, $where);
    }

    /**
     * 获取数量
     * @param $where
     * @return bool|int|mixed|string
     */
    public static function getCount($where)
    {
        return self::count($where);
    }

    /**
     * 更新
     * @param $where
     * @param $data
     * @return int
     */
    public static function updated($where, $data): int
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        return self::update($data, $where)->rowCount();
    }

    /**
     * 新增
     * @param $data
     * @return false|int|mixed|string
     */
    public static function add($data)
    {
        $result = self::insert($data)->rowCount();
        if ($result) {
            return self::id();
        }
        return false;
    }

    /**
     * 获取列表（可扩展条件）
     * @param $store_id
     * @return array
     * @throws \JsonException
     */
    public static function getList($store_id): array
    {
        $where['store_id'] = $store_id;
        $where['check_status'] = self::NORMAL;
        $where['ORDER'] = ['id' => 'DESC'];
        return self::all($where);
    }


    /**
     * 格式化数据
     * @param $data
     * @return array
     * @throws \JsonException
     */
    private static function _format(&$data): array
    {
        if (isset($data['img_id'])) {
            $data['img_id_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $data['img_id'], ImgUrl::IMAGE_SIZE_ORI_NPHONE);
        }
        if (isset($data['open_time']) && $data['open_time']) {
            $open_time = json_decode($data['open_time'], true);
            $data['open_time_format'] = StoreBasicService::formateOpenTime($open_time);
        }
        return $data;
    }
}